package com.adc.douyu_assistant.client;

import com.adc.douyu_assistant.entity.DouyuMessage;
import com.adc.douyu_assistant.entity.DouyuMessageHeader;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.nio.ByteOrder;

@Component
@Order(value = 1)
public class DouyuAssistantClient implements CommandLineRunner {
    private static final DouyuMessageEncoder ENCODER = new DouyuMessageEncoder();

    @Override
    public void run(String... var1) throws Exception {
        String host = "openbarrage.douyutv.com";
        int port = 8601;
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup);
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.option(ChannelOption.TCP_NODELAY, true);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 20480, 0, 4, 0, 0, true));
                    ch.pipeline().addLast(ENCODER);
                    ch.pipeline().addLast(new DouyuMessageDecoder());
                    ch.pipeline().addLast(new DouyuAssistantClientHandler());
                }
            });
            ChannelFuture f = bootstrap.connect(host, port).sync();

            if (f.isSuccess()) {
                System.out.println("start success");
            }

            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

//    public static void main(String[] args) throws Exception {
//        String host = "openbarrage.douyutv.com";
//        int port = 8601;
//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//
//        try {
//            Bootstrap bootstrap = new Bootstrap();
//            bootstrap.group(workerGroup);
//            bootstrap.channel(NioSocketChannel.class);
//            bootstrap.option(ChannelOption.TCP_NODELAY, true);
//            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
//                @Override
//                public void initChannel(SocketChannel ch) throws Exception {
//                    ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 20480, 0, 4, 0, 0, true));
//                    ch.pipeline().addLast(ENCODER);
//                    ch.pipeline().addLast(new DouyuMessageDecoder());
//                    ch.pipeline().addLast(new DouyuAssistantClientHandler());
//                }
//            });
//            ChannelFuture f = bootstrap.connect(host, port).sync();
//
//            if (f.isSuccess()) {
//                System.out.println("success");
//            }
//
//            f.channel().closeFuture().sync();
//        } finally {
//            workerGroup.shutdownGracefully();
//        }
//    }
}
